package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.vo.Cart;
import com.dongdongsop.constant.CookieConstant;
import com.dongdongsop.util.CookieUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("cart")
public class CartController {

    @DubboReference
    private CartService cartService;

    //添加购物车
    @RequestMapping("addCart")
    public String addCart(HttpServletRequest request, HttpServletResponse response, Long skuId, Integer num) {
        //1、先获取购物车数据，如果有数据就追加，没有就创建
        List<Cart> cartList = getCartList(request, response);
        //TODO 3、调用购物车服务，去帮我们追加购物车数据
        cartList = cartService.addCart(cartList, skuId, num);
        //===================登录
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user != null) {
            cartService.addCartListToRedis(cartList, user.getId());
        } else {
            //===================未登录
            //4、保存到cookie
            CookieUtils.setCookie(request, response, CookieConstant.CARTLIST, JSONObject.toJSONString(cartList), true);
        }
        return "success-cart";
    }

    //没登录查询购物车数据
    @RequestMapping("getCartList")
    @ResponseBody
    public List<Cart> getCartList(HttpServletRequest request, HttpServletResponse response) {
        //======================未登录
        //cookie中取出原有的购物车数据
        String cartListString = CookieUtils.getCookieValue(request, CookieConstant.CARTLIST, true);
        //如果取不出来就创建
        if (StrUtil.isBlank(cartListString)) {
            cartListString = "[]";
        }
        //2、将我们取出的cookie中的值，转成List
        List<Cart> cartsList = JSONObject.parseArray(cartListString, Cart.class);
        //=====================登录
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user != null) {
            List<Cart> cartListFormRedis = cartService.selectCartListFromRedis(user.getId());
            if (cartsList.size() > 0) {
                //把cookie的购物车与redis中的购物车合并，然后删除cookie的购物车
                List<Cart> mergeCartList = cartService.mergeCartList(cartsList, cartListFormRedis);
                CookieUtils.deleteCookie(request, response, CookieConstant.CARTLIST);
                // 并将合并后的购物车数据重新放的redis，并返回合并后的数据
                cartService.addCartListToRedis(mergeCartList, user.getId());
                return mergeCartList;
            }
            return cartListFormRedis;
        } else {
            return cartsList;
        }
    }

    //跳转到购物车
    @RequestMapping("toCart")
    public String toCart() {
        return "cart";
    }

    //添加购物车
    @RequestMapping("cartNum")
    @ResponseBody
    public String cartNum(HttpServletRequest request, HttpServletResponse response, Long skuId, Integer num) {
        //1、先获取购物车数据，如果有数据就追加，没有就创建
        List<Cart> cartList = getCartList(request, response);
        //TODO 3、调用购物车服务，去帮我们追加购物车数据
        cartList = cartService.addCart(cartList, skuId, num);
        //===================登录
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user != null) {
            cartService.addCartListToRedis(cartList, user.getId());
        } else {
            //===================未登录
            //4、保存到cookie
            CookieUtils.setCookie(request, response, CookieConstant.CARTLIST, JSONObject.toJSONString(cartList), true);
        }
        return "ok";
    }

    //跳转结算
    @RequestMapping("toGetOrderInfo")
    public String getOrderInfo(){
        return "getOrderInfo";
    }

}
